package com.csdj.service;

import com.csdj.common.CacheContant;
import com.csdj.entity.DevUser;
import com.csdj.entity.DevUserExample;

import com.csdj.mapper.DevUserMapper;
import com.csdj.utils.SecurityUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPubSub;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Transactional
@Service("userService")
public class UserServiceImpl implements  UserService{
    @Resource
    private DevUserMapper mapper;
    @Resource
    private RedisService redisService;

    private final Logger log = Logger.getLogger("UserServiceImpl");

    //构造方法之后执行
    @PostConstruct
    public void init(){
        redisService.subsribe("test", new JedisPubSub() {
            @Override
            public void onMessage(String channel, String message) {
                log.info("channel:"+channel+"   msg:"+message);
                try {
                    TimeUnit.SECONDS.sleep(4);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public long countByExample(DevUserExample example) {
        return 0;
    }

    @Override
    public DevUser login(String devCode, String devPassword) {
        DevUserExample example = new DevUserExample();
        DevUserExample.Criteria criteria = example.createCriteria();
        criteria.andDevCodeEqualTo(devCode)
                .andDevPasswordEqualTo( SecurityUtils.md5Hex( devPassword ) );

        final List<DevUser> list = mapper.selectByExample(example);
        System.out.println("size:"+list.size());
        for (DevUser user:list){
            System.out.println("id:"+user.getId()+",userType="+user.getUserType());
        }
        if(list != null && list.size()>0){
            System.out.println("==登录成功==");
            return list.get(0);
        }
        System.out.println("< 登录失败 >");
        return null;
    }

    public DevUser getById(int id){
        final DevUser user = redisService.getObj(CacheContant.USER_PREFIX + 4, DevUser.class);
        if (user == null){
            //从数据库中查询
            final DevUser userDb = mapper.selectByPrimaryKey(id);
            if(userDb == null){
                return null;
            }
            //把数据保存到缓存
            redisService.setObj(CacheContant.USER_PREFIX+id,userDb,10);
            return userDb;

        }
        return user;
    }


    @Override
    public int register(String devCode, String devPassword) {
        return 0;
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return 0;
    }

    @Override
    public int insert(DevUser record) {
        return 0;
    }

    @Override
    public int insertSelective(DevUser record) {
        return 0;
    }

    @Override
    public List<DevUser> selectByExample(DevUserExample example) {
        return null;
    }

    @Override
    public DevUser selectByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(DevUser record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(DevUser record) {
        return 0;
    }


}
